package com.mxpio.erp.purchase.controller;

import java.io.UnsupportedEncodingException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.mxpio.erp.common.purchase.entity.PurcInvoice;
import com.mxpio.erp.common.purchase.service.PurcInvoiceService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;

@Tag(name = "PurcInvoiceController", description = "采购发票数据接口")
@RestController
@RequestMapping("/erp/purc/pv/")
public class PurcInvoiceController {

    @Autowired
    private PurcInvoiceService purcInvoiceService;

    @GetMapping("list")
    @Operation(summary = "采购发票列表", description = "获取采购发票（无分页）", method = "GET")
    public Result<List<PurcInvoice>> list(Criteria criteria) throws UnsupportedEncodingException {
        List<PurcInvoice> purcInvoices = purcInvoiceService.list(PurcInvoice.class, criteria);
        return Result.OK(purcInvoices);
    }

    @GetMapping("page")
    @Operation(summary = "采购发票列表", description = "获取采购发票（分页）", method = "GET")
    public Result<Page<PurcInvoice>> page(Criteria criteria, Integer pageSize, Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<PurcInvoice> purcInvoices = purcInvoiceService.listPage(PurcInvoice.class, page, criteria);
        return Result.OK(purcInvoices);
    }

    @GetMapping("list/{bizNo}")
    @Operation(summary = "根据bizNo获取采购发票", description = "根据bizNo获取采购发票", method = "GET")
    public Result<PurcInvoice> getById(@PathVariable(name = "bizNo", required = true) String bizNo) {
        PurcInvoice purcInvoice = purcInvoiceService.getById(PurcInvoice.class, bizNo);
        return Result.OK(purcInvoice);
    }

    @PostMapping("add")
    @Operation(summary = "新增采购发票", description = "新增采购发票", method = "POST")
    public Result<PurcInvoice> add(@RequestBody PurcInvoice purcInvoice) {
        purcInvoiceService.save(purcInvoice);
        return Result.OK(purcInvoice);
    }

    @PutMapping("edit")
    @Operation(summary = "编辑采购发票", description = "编辑采购发票", method = "PUT")
    public Result<PurcInvoice> edit(@RequestBody PurcInvoice purcInvoice) {
        purcInvoiceService.update(purcInvoice);
        return Result.OK(purcInvoice);
    }

    @DeleteMapping("remove/{bizNo}")
    @Operation(summary = "根据bizNo删除采购发票", description = "根据bizNo删除采购发票,多个bizNo用英文逗号分隔", method = "DELETE")
    public Result<PurcInvoice> remove(@PathVariable(name = "bizNo", required = true) String bizNos) {
        String[] bizNo = bizNos.split(",");
        for(String key : bizNo){
            purcInvoiceService.remove(PurcInvoice.class, key);
        }
        return Result.OK();
    }
}
